﻿using System;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

namespace Medianamik.UI.Controls
{
    public class TelephoneTextBox : BaseMedianamikControl
    {
        protected const string TelephoneRegex = @"^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$"; // @"^[0-9]{3}?-[0-9]{3}-[0-9]{4}$";

        private readonly TextBox _txtExtensionPart = new TextBox
        {
            ID = ControlToValidateId,
            MaxLength = 3,
            CssClass = "TxtBox",
            Width = Unit.Pixel(30)
        };
        private readonly TextBox _txtFirstPart = new TextBox
        {
            ID = ControlToValidateId + "_2",
            MaxLength = 3,
            CssClass = "TxtBox",
            Width = Unit.Pixel(40)
        };
        private readonly TextBox _txtSecondPart = new TextBox
        {
            ID = ControlToValidateId + "_3",
            MaxLength = 4,
            CssClass = "TxtBox Last",
            Width = Unit.Pixel(40)
        };
        private readonly CustomValidator _cvTelephone = new CustomValidator
        {
            ID = ControlToValidateId + "_validator"
        };

        public BasePage BasePage
        {
            get { return (BasePage) base.Page; }
        }

        public override object GetValue()
        {
            return string.Format("{0}-{1}-{2}",
                _txtExtensionPart.Text,
                _txtFirstPart.Text,
                _txtSecondPart.Text).Replace("--", string.Empty);
        }

        public override void SetValue(object value)
        {
            _txtExtensionPart.Text = string.Empty;
            _txtFirstPart.Text = string.Empty;
            _txtSecondPart.Text = string.Empty;

            if (value != null)
            {
                string phoneNumber = Regex.Replace(value.ToString(), @"[\D]", "");
                if (phoneNumber.Length == 10)
                {
                    _txtExtensionPart.Text = phoneNumber.Substring(0, 3);
                    _txtFirstPart.Text = phoneNumber.Substring(3, 3);
                    _txtSecondPart.Text = phoneNumber.Substring(6, 4);
                }
            }
        }

        public override Type TypeOfValue
        {
            get { return typeof(string); }
        }

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            Controls.Add(_txtExtensionPart);
            Controls.Add(_txtFirstPart);
            Controls.Add(_txtSecondPart);
            Controls.Add(_cvTelephone);

            _cvTelephone.ServerValidate += _cvTelephone_ServerValidate;
            _cvTelephone.ValidationGroup = DefaultValidationGroupName;
            _cvTelephone.ErrorMessage = string.Format("{0} {1}", Label, REGEX);
            _cvTelephone.Display = ValidatorDisplay.None;

            if (pUseDataSource)
                SetValue(DataSource);
        }

        protected void _cvTelephone_ServerValidate(object source, ServerValidateEventArgs args)
        {
            var phone = GetValue().ToString();
            if (!string.IsNullOrEmpty(phone))
                args.IsValid = Regex.Match(phone, TelephoneRegex).Success;
        }
    }
}
